<?php
require_once('security.php');
 
  if ((isset($_POST['submit'])) && ($_POST['submit'] != 1) ) 
  {

    // Split filename string into array using "." separator
    $extension = explode ('.', $_FILES['upload']['name']);
    // Create new filename with original file extension added
    // modified by ljohns 31Aug08 original code not working missing file name
    // also added remove an extension if supplied to maintain integ of file type
    $newFileName = explode('.',$_POST['filename']);
    $filename = $newFileName[0] . '.' . $extension[1];
	$imageText = $_POST['imageText'];
	
    // Move the file from temp folder to images folder
    if (move_uploaded_file($_FILES['upload']['tmp_name'], "../imgtxt/$filename")) {
      $response = 'The file named <b>' . $filename . '</b> has been uploaded.';
      $display = '<br /><img src="../imgtxt/' . $filename . '" />';
	  $displayImageText = '<br />The file named <b>' . $newFileName[0] . '.txt' . '</b> has been uploaded.';
	  
	  $fh = fopen("../imgtxt/$newFileName[0].txt", "w");
	  
	  fwrite($fh, $imageText);
	  
	  fclose($fh);
	  
    } else {
      $response = 'The file could not be moved.';
    }
  }

?>

<?php $thisPage = "upload"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload a file</title>
<meta name="robots" content="noindex,nofollow" />
<?php require_once('theme/admintheme.php');?>
</head>
<body>

<div id="wrap">

<?php include_once('navigation.php'); ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Select file on hard drive:<br />
<input type="file" name="upload" /><br />
Name field for Image text : <br />
<textarea name="imageText" rows="10"></textarea><br />
Name file (omit file extension):<br />
<input size="20" name="filename" maxlength="30" />
<input type="hidden" name="MAX_FILE_SIZE" value="20000" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php if (isset($response)) {echo $response;} ?>
<?php if (isset($displayImageText)) {echo $displayImageText;} ?>
<?php if (isset($display)) {echo $display;} ?>


<?php

  /*echo '<br /><p><b>Current image files:</b></p>';
  $dirname = "../images";  
  
  $dh = opendir($dirname);
  while (false !== ($file = readdir($dh))) {
    if ((strstr($file, '.jpg')) || (strstr($file, '.bmp'))|| (strstr($file, '.jpeg')) || (strstr($file, '.gif')) || (strstr($file, '.png'))) {
      print '<a href="../images/' . $file . '" title="View">' . $file . '</a><br />';
    }
  }
  closedir($dh);*/
  
  echo '<br /><p><b>Image and text files that are in imgtxt folder:</b></p>';
  $dirname1 = "../imgtxt";  
  
  $dh1 = opendir($dirname1);
  while (false !== ($file1 = readdir($dh1))) {
    if ((strstr($file1, '.txt')) || (strstr($file1, '.jpg')) || (strstr($file1, '.bmp'))|| (strstr($file1, '.jpeg')) || (strstr($file1, '.gif')) || (strstr($file1, '.png'))) {
      print '<a href="../imgtxt/' . $file1 . '" title="ViewText">' . $file1 . '</a><br />';
    }
  }
  closedir($dh1);
  

?>

</div>

</body>
</html>
